package de.baumann.browser.js

import android.webkit.JavascriptInterface
import org.json.JSONException
import org.json.JSONObject


class VaptchaInterface(var vaptchaCallback: VaptchaCallback) {
    val PASS = "pass" //通过

    val CANCEL = "cancel" //取消


    @JavascriptInterface
    fun signal(json: String) {
        //json格式{signal:"",data:""}
        //signal: pass (通过) ； cancel（取消）
        try {
            val jsonObject = JSONObject(json)
            val signal = jsonObject.getString("signal")
            if (PASS == signal) { //通过
                vaptchaCallback.pass(jsonObject.getString("data"))
            } else if (CANCEL == signal) { //取消
                vaptchaCallback.cancel()
            }
        } catch (e: JSONException) {
            e.printStackTrace()
        }
    }

    interface VaptchaCallback{
        fun pass(token:String)
        fun cancel()
    }

}